package com.example.gank.mvp.presenter;

import com.android.core.base.BasePresenter;
import com.android.core.network.ProgressSubscriber;
import com.android.core.network.RestApi;
import com.android.core.network.RetryWhenNetworkException;
import com.android.core.network.SubscriberOnNextListener;
import com.android.core.network.TransformUtils;
import com.example.gank.api.ApiService;
import com.example.gank.api.ResponseFunc;
import com.example.gank.bean.Result;
import com.example.gank.bean.WelfareGankBean;
import com.example.gank.mvp.view.WelfareView;

import java.util.List;

import rx.Subscription;

/**
 * Created by bsx on 17-1-9.
 */

public class WelfarePresenter extends BasePresenter<WelfareView> {

    public void getWelfare(int page) {
        Subscription subscribe = RestApi.getInstance().create(ApiService.class)
                .getWelfareGank(page)
                .compose(TransformUtils.<Result<List<WelfareGankBean>>>defaultSchedulers())
                .map(new ResponseFunc<List<WelfareGankBean>>())
                .retryWhen(new RetryWhenNetworkException())
                .subscribe(new ProgressSubscriber<List<WelfareGankBean>>(new SubscriberOnNextListener<List<WelfareGankBean>>() {
                    @Override
                    public void onNext(List<WelfareGankBean> welfareGankBeen) {
                        if (welfareGankBeen != null && !welfareGankBeen.isEmpty()) {
                            getView().onSucceed(welfareGankBeen);
                        } else {
                            getView().onFail("请求数据为空");
                        }
                    }

                    @Override
                    public void onFail(Throwable e) {
                        getView().onFail("error:" + e.getMessage() + "\n" + "请求失败，请稍后重试");
                    }
                }, mActivity));
        addSubscribe(subscribe);
    }
}
